package com.zhiyou100.api.math_demo;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;

public class Demo2 {

    public static void main(String[] args) {
//        testBigInteger();
        testDecimal();
    }

    //大数值（小数）   一般做精度计算时使用
    public static void testDecimal() {
        BigDecimal bd1 = new BigDecimal("23.0");
        BigDecimal bd2 = new BigDecimal("56.234579");
        BigDecimal add = bd1.add(bd2);
        System.out.println("add = " + add);
        double a = 23.0;
        double b = 56.234579;
        System.out.println("a + b =" + (a + b));
        System.out.println("a / b = " + (a / b));
        //设定取小数点后几位  向上取
        BigDecimal d = bd1.divide(bd2, 5, BigDecimal.ROUND_CEILING);
        System.out.println("d = " + d);
        //设定取小数点后几位  四舍五入
        BigDecimal d2 = bd1.divide(bd2, 5, RoundingMode.HALF_UP);
        System.out.println("d2 = " + d2);

    }


    //大数值： BigInteger   BigDecimal
    public static void testBigInteger() {
        BigInteger bi = new BigInteger("789456478789");
        BigInteger bi2 = new BigInteger("45631234567897");
        BigInteger add = bi.add(bi2);  //加法
        System.out.println("add = " + add);
        BigInteger subtract = bi.subtract(bi2);  //减法
        System.out.println("subtract = " + subtract);
        BigInteger m = bi.multiply(bi2);  //乘法
        System.out.println("m = " + m);
        BigInteger d = m.divide(bi);//除法
        System.out.println("d = " + d);
        BigInteger r = bi2.remainder(bi); //取余
        System.out.println("r = " + r);
        int i = bi.compareTo(bi2);// 比较大小
        System.out.println("i = " + i);
    }

}
